﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using PoolGame2.Player;

namespace PoolGame2.Menus
{
    public class MainMenu : Menu
    {
        public MainMenu(PoolGame game)
            : base(game)
        {
            int height = 60;
            int menuspacing = 70;
            MenuItem item = new MenuItem("Start Debug Game", game);
            item.Position = new Vector2(40, height);
            item.onSelected = new EventHandler(debugSelected);
            AddMenuItem(item);

            //height += (item.GetHeight() * 2);
            height += menuspacing;

            item = new MenuItem("Join Game", game);
            item.Position = new Vector2(40, height);
            item.onSelected = new EventHandler(joinSelected);
            AddMenuItem(item);

            //height += (item.GetHeight() * 2);
            height += menuspacing;

            item = new MenuItem("Host Game", game);
            item.Position = new Vector2(40, height);
            item.onSelected = new EventHandler(hostSelected);
            AddMenuItem(item);

            //height += (item.GetHeight() * 2);
            height += menuspacing;

            item = new MenuItem("Credits", game);
            item.Position = new Vector2(40, height);
            item.onSelected = new EventHandler(creditsSelected);
            AddMenuItem(item);

            //height += (item.GetHeight() * 2);
            height += menuspacing;

            item = new MenuItem("Exit", game);
            item.Position = new Vector2(40, height);
            item.onSelected = new EventHandler(exitSelected);
            AddMenuItem(item);
        }

        public void debugSelected(object caller, EventArgs e)
        {
            PoolGame pgame = (PoolGame)Game;
            pgame.InitialiseGame(true);
        }

        public void joinSelected(object caller, EventArgs e)
        {
            PoolGame pgame = (PoolGame)Game;
            pgame.menuManager.SwitchMenu(MenuManager.eMenuTypes.MENU_SEARCH);
        }

        public void hostSelected(object caller, EventArgs e)
        {
            PoolGame pgame = (PoolGame)Game;
            NetworkSessionType sessType = NetworkSessionType.SystemLink;
            foreach( PlayerPresence player in pgame.signedInPlayers )
            {
                SignedInGamer gamer = player.GetSignedInGamer();
                if (gamer != null)
                {
                    if (gamer.IsSignedInToLive)
                        sessType = NetworkSessionType.PlayerMatch;
                }
            }
            pgame.networkManager.HostSession(sessType);
        }

        public void creditsSelected(object caller, EventArgs e)
        {
            PoolGame pgame = (PoolGame)Game;
            pgame.menuManager.SwitchMenu(MenuManager.eMenuTypes.MENU_CREDITS);
        }

        public void exitSelected(object caller, EventArgs e)
        {
            Game.Exit();
        }
    }
}
